/*
 * @lc app=leetcode.cn id=24 lang=cpp
 *
 * [24] 两两交换链表中的节点
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <stdlib>
#include <queue>
#include <map>
#include <set>
#include <umordered_map>
#include <umordered_set>
#include <cmath>

using namespace std;
// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* preHead = new ListNode(0,head);
        auto cur = preHead;
        while(cur->next && cur->next->next) {
            auto a = cur->next;
            auto b = cur->next->next;
            a->next = b->next;
            b->next = a;
            cur->next = b;
            cur = cur->next->next;
        }
        return preHead->next;
    }
};
// @lc code=end

